package jdbc.lia.edu.ar;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Main {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			Class.forName("com.mysql.jdbc.Driver");
			Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/basedatos","root","root");			
			Statement stmt = con.createStatement();		  
			
			//Crear Tabla
			//stmt.executeUpdate("Create Table Persona(id int(4) NOT NULL,nombre varchar(45),apellido varchar(45),edad int(4))");
			
			
			   /** inserting Rows */

            int i=stmt.executeUpdate(" insert into Persona(id,nombre,apellido,edad)values(1,'Horacio', 'Muñoz',32)");
            stmt.execute("select *  from Persona");
            
            
            String sql = "SELECT * FROM Persona";
            ResultSet rs = stmt.executeQuery(sql);
            //STEP 5: Extract data from result set
            while(rs.next()){
               //Retrieve by column name
               int id  = rs.getInt("id");
               int age = rs.getInt("edad");
               String first = rs.getString("nombre");
               String last = rs.getString("apellido");

               //Display values
               System.out.print("ID: " + id);
               System.out.print(", Age: " + age);
               System.out.print(", First: " + first);
               System.out.println(", Last: " + last);
            }
            rs.close();
            
            
            con.close();
			
		} catch (ClassNotFoundException | SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		 

        /** Getting Connection*/



	}

}
